package vip.xiaonuo.visitor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import vip.xiaonuo.PushCallBack;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.facedoor.modular.message.SnowyMessageTemplate;
import vip.xiaonuo.facedoor.modular.message.Topic;
import vip.xiaonuo.facedoor.modular.message.domain.Message;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.param.VisitorParam;
import vip.xiaonuo.facedoor.modular.visitor.service.FacedoorVisitorService;
import vip.xiaonuo.modular.threemessagetable.entity.ThreeMessageTable;
import vip.xiaonuo.modular.threemessagetable.service.ThreeMessageTableService;

import javax.annotation.Resource;


@Component
@RocketMQMessageListener(
        topic = Topic.FACE_DOOR_VISITOR_DELETE,                      // 1.topic：消息的发送者使用同一个topic
        consumerGroup = Topic.FACE_DOOR_VISITOR_DELETE,               // 2.group：不用和生产者group相同 ( 在RocketMQ中消费者和发送者组没有关系 )
        selectorExpression = "${tag}",                   // 3.tag：设置为 * 时，表示全部。
        messageModel = MessageModel.CLUSTERING    // 4.消费模式：默认 CLUSTERING （ CLUSTERING：负载均衡 ）（ BROADCASTING：广播机制 ）
)
public class VisitorDeleteListener implements RocketMQListener<MessageExt> {


    @Resource
    private FacedoorVisitorService visitorService;

    @Resource
    private SnowyMessageTemplate snowyMessageTemplate;


    @Resource
    private ThreeMessageTableService threeMessageTableService;

    @Override
    public void onMessage(MessageExt messageExt) {

        byte[] body = messageExt.getBody();
        String msg = new String(body);

        ThreeMessageTable threeMessageTable = new ThreeMessageTable();
        threeMessageTable.setTopic(Topic.FACE_DOOR_VISITOR_DELETE);
        threeMessageTable.setMsg(msg);
        threeMessageTableService.save(threeMessageTable);
        Visitor visitor = JSON.parseObject(msg, Visitor.class);
        try {
            VisitorParam visitorParam = new VisitorParam();
            BeanUtil.copyProperties(visitor, visitorParam);
            visitorService.delete(visitorParam);
            PushCallBack.Detail detail = new PushCallBack.Detail();
            detail.setMessage("success");
            detail.setId(visitor.getId());
        } catch (Exception e) {
            PushCallBack.Detail detail = new PushCallBack.Detail();
            detail.setMessage(ExceptionUtil.getMessage(e));
            detail.setId(visitor.getId());
            threeMessageTable.setReturnMsg(JSON.toJSONString(ExceptionUtil.getMessage(e)));
            threeMessageTableService.updateById(threeMessageTable);
        }
    }

}


